/*
    Heart of Gold engine management system
    Copyright (C) 2004 Heart of Gold development team

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package com.heartOfGold.view;

/*
 The mixture table setup page for the Megasquirt HOG_Component
 by Ryan Higgins
 May 2004
 */
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.GridLayout;
import java.awt.Dimension;
//import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.BorderFactory;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.Box;

import com.heartOfGold.controller.Fuel;

import java.io.*;

public class FuelTable extends JPanel implements ActionListener {
    private Fuel model;
    private FuelMaster master;
    private JPanel table, mapBins, rpmBins, buttons;
    private JTextField[] fields, mapFields, rpmFields;
    private int[] values, mapValues, rpmValues;
    private Dimension fieldSize;
    private JButton get, send, save, done;
    private Box alignV;
    private Box alignVMap;
    private Box alignH;

    public FuelTable(final FuelMaster m,
            final Fuel mod) {
        model = mod;
        master = m;
        values = new int[64];
        mapValues = new int[8];
        rpmValues = new int[8];

        fieldSize = new Dimension(45, 25);

        // border
        setBorder(BorderFactory.createTitledBorder("Fuel Mixture Table"));

        // setup the table
        table = new JPanel(new GridLayout(8, 8));
        fields = new JTextField[64];
        for (int i = 0; i < 64; ++i) {
            fields[i] = new JTextField();
            (fields[i]).setPreferredSize(fieldSize);
            (fields[i]).setHorizontalAlignment(JTextField.CENTER);
            // table.add(fields[i]);
        }
        int n = 64;
        for (int i = 0; i < 8; i++) {
            n -= 8;
            for (int j = 0; j < 8; j++) {
                table.add(fields[n + j]);
            }
        }
        table.setBorder(BorderFactory.createTitledBorder(
                "Fuel Mixture as % of Volumetric Efficiency"));

        // setup the MAP bins
        mapBins = new JPanel(new GridLayout(8, 1));
        mapFields = new JTextField[8];
        for (int i = 0; i < 8; i++) {
            mapFields[i] = new JTextField();
            (mapFields[i]).setPreferredSize(fieldSize);
            (mapFields[i]).setHorizontalAlignment(JTextField.CENTER);
        }
        for (int i = 8; i > 0; i--) {
            mapBins.add(mapFields[i - 1]);
        }
        mapBins.setBorder(BorderFactory.createTitledBorder("MAP"));

        // setup the RPM bins
        rpmBins = new JPanel(new GridLayout(1, 8));
        rpmFields = new JTextField[8];
        for (int i = 0; i < 8; i++) {
            rpmFields[i] = new JTextField();
            (rpmFields[i]).setPreferredSize(fieldSize);
            (rpmFields[i]).setHorizontalAlignment(JTextField.CENTER);
            rpmBins.add(rpmFields[i]);
        }
        rpmBins.setBorder(BorderFactory.createTitledBorder("RPM"));

        // setup buttons
        buttons = new JPanel();
        get = new JButton("Get Table");
        send = new JButton("Send Table");
        done = new JButton("Done");
        get.addActionListener(this);
        send.addActionListener(this);
        done.addActionListener(this);
        buttons.add(get);
        buttons.add(send);
        buttons.add(done);
        // buttons.setBorder(BorderFactory.createEtchedBorder());

        // Assemble the table
        alignV = Box.createVerticalBox();
        alignH = Box.createHorizontalBox();
        alignVMap = Box.createVerticalBox();
        int height = (int) (rpmBins.getPreferredSize()).getHeight();
        int width = (int) (mapBins.getPreferredSize()).getWidth();
        Dimension rig = new Dimension(width, height);
        alignVMap.add(mapBins);
        alignVMap.add(Box.createRigidArea(rig));
        alignH.add(alignVMap);
        alignV.add(table);
        alignV.add(rpmBins);
        alignH.add(alignV);

        // add components
        Box alignAll = Box.createVerticalBox();
        alignAll.add(alignH);
        alignAll.add(buttons);

        add(alignAll);
    }

    public final void actionPerformed(final ActionEvent ae) {
        //TODO populate catch statements
        String command = ae.getActionCommand();
        if (command.equals("Get Table")) {
            try {
                model.getConstants();
            } catch (IOException e) {
            }
            return;
        }

        if (command.equals("Send Table")) {
            try {
                addChange();
                model.writeChanges();
                model.burn();
            } catch (IOException e) {
            }
            return;
        }

        if (command.equals("Done")) {
            master.switchScreen("Main");
            return;
        }

        JOptionPane.showMessageDialog(this,
                "This function is not yet implemented", "Not Implemented",
                JOptionPane.INFORMATION_MESSAGE);
    }

    public final void update(final int[] constants) {
        for (int i = 0; i < 64; i++) {
            fields[i].setText("" + constants[i]);
            values[i] = constants[i];
        }
        for (int i = 108; i < 116; i++) {
            mapFields[i - 108].setText("" + constants[i]);
            mapValues[i - 108] = constants[i];
        }
        for (int i = 100; i < 108; i++) {
            rpmFields[i - 100].setText("" + (constants[i] * 100));
            rpmValues[i - 100] = constants[i] * 100;
        }
    }

    public final void addChange() {
        int n = 0;
        for (int i = 0; i < 64; i++) {
            n = (Integer.valueOf(fields[i].getText())).intValue();
            if (n != values[i]) {
                model.addChange(i, n);
            }
        }
        for (int i = 108; i < 116; i++) {
            n = (Integer.valueOf(mapFields[i - 108].getText())).intValue();
            if (n != mapValues[i - 108]) {
                model.addChange(i, n);
            }
        }
        for (int i = 100; i < 108; i++) {
            n = (Integer.valueOf(rpmFields[i - 100].getText())).intValue();
            if (n != rpmValues[i - 100]) {
                model.addChange(i, n);
            }
        }

    }
}
